<?php
/**
 * --
 *
 * @author Flan fzm010@hotmail.com
 * @version 2.0.0 2012-11-16
 */ 

/*
1.  通常来说  group by 比  distinct的效率高
	
	如果2种可以同时使用的地方  建议使用 group by 传说distinct会全表扫描
	
2.  group by 和 distinct 的同时使用   count 计算数量

	f1.gid  f1.vid 
		1     2
		1     3
		2     2
		2     4
		3     5
	
	
	f2.gid  f2.pid
		1     5
		1     8
		2     9
		2     8
		3     7
		3     6
	
	
	#单一精确值
	SELECT f1.vid, COUNT(f1.gid)
	FROM f1
	GROUP BY f1.vid
	
	#多表联合 数据值得到放大
	SELECT f1.vid, COUNT(f1.gid)
	FROM f1
	JOIN f2 ON f1.gid = f2.gid
	GROUP BY f1.vid
	
	#多表联合的精确值
	SELECT f1.vid, COUNT(DISTINCT f1.gid), f2.pid
	FROM f1
	JOIN f2 ON f1.gid = f2.gid
	GROUP BY f1.vid
	
3. 跨行查询   查询f1.vid=2 and f1.vid=3

	#单表精确
	SELECT f1.gid
	FROM f1
	WHERE f1.vid = 2 OR f1.vid=3
	GROUP BY f1.gid
	HAVING(COUNT(f1.vid))=2
	
	#多表联合 数据值得到放大
	SELECT f1.gid
	FROM f1
	JOIN f2 ON f1.gid = f2.gid
	WHERE f1.vid = 2 OR f1.vid=3
	GROUP BY f1.gid
	HAVING(COUNT(f1.vid))=2
	
	#多表联合的精确值
	SELECT f1.gid
	FROM f1
	JOIN f2 ON f1.gid = f2.gid
	WHERE f1.vid = 2 OR f1.vid=3
	GROUP BY f1.gid
	HAVING(COUNT(DISTINCT f1.vid))=2